package com.example.software.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.software.constant.MessageConstant;
import com.example.software.constant.StatusConstant;
import com.example.software.dto.doctorLoginDTO;
import com.example.software.entity.MedicineEntity;
import com.example.software.exception.AccountLockedException;
import com.example.software.exception.AccountNotFoundException;
import com.example.software.exception.PasswordErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.software.utils.PageUtils;
import com.example.software.utils.Query;

import com.example.software.dao.DoctorDao;
import com.example.software.entity.DoctorEntity;
import com.example.software.service.DoctorService;
import org.springframework.util.DigestUtils;


@Service("doctorService")
public class DoctorServiceImpl extends ServiceImpl<DoctorDao, DoctorEntity> implements DoctorService {


    @Autowired
    private DoctorDao doctorDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

//        System.out.println("params:"+params);
//        System.out.println("params:"+params.get("page"));
//        System.out.println("params:"+params.get("limit"));

        LambdaQueryWrapper<DoctorEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.containsKey("departmentId")) {

            Long departmentId = (Long) params.get("departmentId");
           // System.out.println("medicineTypeId"+departmentId);
            wrapper.eq(departmentId != null, DoctorEntity::getDepartmentId, departmentId);
        }


        if (params.containsKey("name")) {

            String name = (String) params.get("name");
            wrapper.like(name != null, DoctorEntity::getName, name);

        }


        IPage<DoctorEntity> page = this.page(
                new Query<DoctorEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public DoctorEntity login(doctorLoginDTO doctorDTO) {

        String username = doctorDTO.getUsername();
        String password = doctorDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        DoctorEntity doctorEntity = doctorDao.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (doctorEntity == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(doctorEntity.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (doctorEntity.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return doctorEntity;

    }

}